#include "AlarmTimer.h"
#include <error.h>
#include <errno.h>
#include <pthread.h>


void AlarmTimerOnce(AlarmTimerCallback cb, int second)
{
  signal(SIGALRM, cb);
  alarm(second);
}


//cb pthread_detach(pthread_self());
timer_t  TimerCreate(TimerHandle cb, int microsecond)
{
    struct sigevent evp;  
    struct itimerspec ts;  
    timer_t timer;  
    int ret;  
  
    memset (&evp, 0, sizeof (evp));  
    evp.sigev_value.sival_ptr = &timer;  
    evp.sigev_notify = SIGEV_THREAD;  
    evp.sigev_notify_function = cb;  
    evp.sigev_value.sival_int = 666; //作为handle()的参数  
    
     ret = timer_create(CLOCK_MONOTONIC /*CLOCK_MONOTONIC*/, &evp, &timer);  
    if(ret == -1)
    {
       printf("timer create failed\n");
       return -1;
    } 
    ts.it_interval.tv_sec = microsecond/1000000;  
    ts.it_interval.tv_nsec = microsecond%1000;  
    ts.it_value.tv_sec =  microsecond/1000000;  
    ts.it_value.tv_nsec = microsecond%1000;       
    ret = timer_settime(timer, 0, &ts, NULL);   
    if(ret == -1)
    {
       printf("timer create failed %s\n",strerror(errno));
       return -1;
    }    
    return   timer;
}



int  TimerDelete(timer_t timer)
{

    return timer_delete(timer);

}